#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;

int t;
string s;

int f[MAXN], g[MAXN];
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> t;
    while (t--) {
        cin >> s;
        int n = s.size();
        s = "$" + s;

        for (int i = 1; i <= n; i++) {
            f[i] = f[i - 1] + (s[i] == '1');
            g[i] = g[i - 1] + (s[i] == '0');
        }

        int ans = 1e9;
        for (int i = 1; i <= n; i++) {
            int k = i + f[n] - 1;
            int y;
            // 000000000
            if (i > k) {
                ans = 0;
                break;
            }

            //[i, n]
            if (k > n) {
                ans = min(ans, max(f[i-1], g[n]-g[i-1]));
            } else {
                ans = min(ans, max(f[i-1]-f[k]+f[n], g[k]-g[i-1]));
            }

        }
        cout << ans << endl;
    }
    return 0;
}